﻿using EkStudio.com.bean.xml.RootInterface.Plugin;
using System;
using System.Reflection;

namespace EkStudio.com.ui.unitbill.property
{
    public class BillUiJavaToProperty
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiJavaToProperty));

        private BillExpandJavaCodeItem java = null;
        private BillUiJavaToProperty[] Array = null;
        private bool Simple = true;

        public BillUiJavaToProperty(BillExpandJavaCodeItem _java)
        {
            this.java = _java;
        }

        public BillUiJavaToProperty(BillUiJavaToProperty[] array)
        {
            Array = array;
            this.Simple = false;
        }


        public string Key
        {
            get { return Simple ? java.Key : ""; }
            set { if (Simple) java.Key = value; }
        }
        public string Caption
        {
            get { return Simple ? java.Caption:""; }
            set { if (Simple) java.Caption = value; }
        }

        public bool Enable
        {
            get { return Simple ? java.Enable : GetBoolValue("Enable"); }
            set { if (Simple) java.Enable = value; else SetValue("Enable", value); }
        }

        public string PluginType
        {
            get { return Simple ? java.PluginType : GetStringValue("PluginType"); }
            set { if (Simple) java.PluginType = value;SetValue("PluginType", value); }
        }

        public string Content
        {
            get { return Simple ? java.Content : ""; }
            set { if (Simple) java.Content = value; }
        }

        public BillExpandJavaCodeItem JavaItem
        {
            get { return Simple ? java : null; }
        }

        public string EditText
        {
            get { return "[JavaExp]"; }
            set { }
        }


        private object SetValue(string key, object value)
        {
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiJavaToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiJavaToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null)
                    {
                        Property.SetValue(data, value);
                    }
                }
            }
            return value;
        }

        private bool GetBoolValue(string key)
        {
            bool result = true;
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiJavaToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiJavaToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(bool))
                    {
                        object val = Property.GetValue(data);
                        if (val.Equals(false))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        private int GetIntValue(string key)
        {
            int result = 0;
            if (this.Array != null && this.Array.Length > 0)
            {
                BillUiJavaToProperty data = Array[0];
                Type ClazzType = typeof(BillUiJavaToProperty);
                PropertyInfo Property = ClazzType.GetProperty(key);
                if (Property != null && Property.GetMethod.ReturnType == typeof(int))
                {
                    object val = Property.GetValue(data);
                    if (val is int)
                    {
                        result = (int)val;
                    }
                }
            }
            return result;
        }

        private string GetStringValue(string key)
        {
            string result = "";
            if (this.Array != null && this.Array.Length > 0)
            {
                for (int i = 0; i < this.Array.Length; i++)
                {
                    BillUiJavaToProperty data = Array[i];
                    Type ClazzType = typeof(BillUiJavaToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(string))
                    {
                        object val = Property.GetValue(data);
                        if (val is string)
                        {
                            if (result == "") result = val.ToString();
                            else if (result != (string)val)
                            {
                                result = "";
                                break;
                            }

                        }
                    }
                }
            }
            return result;
        }
    }
}
